<?php

namespace think\paginator\driver;
/**
 * 自定义分页
 * @author mixsun
 */


class MyBootstrap extends Bootstrap{
 
    /**
     * 首页
     * @param string $text 自定义显示文字
     * @return string
     */
    protected function getFirstButton($text = "&laquo;"){
        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($text);
        }
        $url = $this->url(1);
        return $this->getPageLinkWrapper($url, $text);
    }
 
    /**
     * 末页
     * @param string $text 自定义显示文字
     * @return string
     */
    protected function getLastButton($text = "&raquo;"){
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }
        $url = $this->url($this->lastPage());
        return $this->getPageLinkWrapper($url, $text);
    }
    /**
     * 获取 共 多少 页信息
     * @return string
     */
    protected function getTotalInfo()
    {
        return '<li class="total"><span>共' . $this->total() .'条，当前'.$this->currentPage().'/'.$this->lastPage().' 页</span></li>';
    }
    /**
     * 渲染分页html
     * @return mixed
     */
    public function render(){
        if ($this->hasPages()) {
            if ($this->simple) {
                return sprintf(
                    '<ul class="pager">%s %s %s %s</ul>',
                    $this->getFirstButton("&laquo;"),
                    $this->getPreviousButton("&lsaquo;"),
                    $this->getNextButton("&rsaquo;"),
                    $this->getLastButton("&raquo;")
                );
            } else {
                return sprintf(
                    '<ul class="pagination">%s %s %s %s %s %s</ul>',
                    $this->getTotalInfo(),
                    $this->getFirstButton("&laquo;"),
                    $this->getPreviousButton("&lsaquo;"),
                    $this->getLinks(),
                    $this->getNextButton("&rsaquo;"),
                    $this->getLastButton("&raquo;"),
                    $this->total(),
                    $this->currentPage(),
                    $this->lastPage()
                );
            }
        }else{
            if ($this->simple) {
 
            } else {
                return sprintf(
                    '<ul class="pagination">%s</ul>',
                    $this->getTotalInfo(),
                    $this->total(),
                    $this->currentPage(),
                    $this->lastPage()
                );
            }
        }
    }
 
    public function toArray(){
        try {
            $total = $this->total();
        } catch (\DomainException $e) {
            $total = null;
        }
        return [
            'total'        => $total,
            'last_page'    => $this->lastPage(),
            'per_page'     => $this->listRows(),
            'current_page' => $this->currentPage(),
            'data'         => $this->items->toArray(),
            'render'       => $this->render()
        ];
    }
 
}